package RTL.Task3;

//准备一个 HashMap 集合，统计字符串"123,456,789,123,456"中每个数字字符串出现的次数并打印出来。


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapTest {
    public static void main(String[] args) {
        //声明hashmap集合，然后放入数组
        Map<String, Integer> m1 = new HashMap<>();
        String inputString = "123,456,789,123,456";
        String[] strArray = inputString.split(",");
        //循环遍历放入hashmap
        for (String str : strArray) {
            if(m1.containsKey(str)) {
                m1.put(str, m1.get(str) + 1);
            } else {
                m1.put(str, 1);
            }
        }
        //使用iterator即可直接输出出现次数
        Iterator<Map.Entry<String, Integer>> iterator = m1.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Integer> entry = iterator.next();
            System.out.println(entry.getKey() + " = " +entry.getValue());
        }
    }
}
